﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wheeler.Common.Extensions;
using Wheeler.Domain;
using Wheeler.Web.ViewModels;
using Wheeler.Web.Utility;
using AutoMapper;

namespace Wheeler.Web.Areas.Private.Controllers
{
    [Authorize(Roles = "Operator,Admin")]
    public class AccountController : BaseController
    {
		private WheelerEntities _Context = new WheelerEntities();
        
		#region Constructors
		public AccountController()
		{
		}

		public AccountController(UserViewModel currentUser)
			: base(currentUser)
        {
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return RedirectToAction("dashboard");
        }
        public ActionResult DashBoard()
        {
            if (UserInfo.IsAdmin)
                return RedirectToAction("Dashboard", "Account", new { area = AppConstants.Area.ADMIN });            
            else
                return View();

        }

        public ActionResult ChangePassword(String IsSaved = null)
        {
            ChangePasswordModel viewModel = new ChangePasswordModel();
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (!ModelState.IsValid)
                return View(model);

            User entity = _Context.Users.FirstOrDefault(P => P.ID == LoggedInUser.ID);
            if(String.Compare(entity.Password, model.OldPassword, true) != 0)
            {
                ModelState.AddModelError("OldPassword", "Your old password do not match!");
                return View(model);
            }

            entity.Password = model.NewPassword;
            _Context.SaveChanges();
            return RedirectToAction("ChangePassword", new {IsSaved = "True" });
        }
		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
